/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package server;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author apple
 */
public class MarketImpl extends UnicastRemoteObject implements Market{
    private String marketName;
    private Map<String, Trader> traders= new HashMap<String,Trader>();
    public MarketImpl(String marketName) throws RemoteException{
        super();
        this.marketName=marketName;
    } 

    @Override
    public synchronized Trader newTrader(String name) throws RemoteException {
        TraderImpl trader = (TraderImpl) traders.get(name);
        if(trader != null){
            System.out.println("Trader [" + name + "] exist!");
            throw new UnsupportedOperationException("Not supported yet.");
        }
        trader = new TraderImpl(name);
        traders.put(name, trader);
        System.out.println("Market:"+marketName+"Trader:"+ trader + "has been created for" +name);
        return trader;
    }

    @Override
    public synchronized Trader getTrader(String name){
        
        return traders.get(name);
    }

    @Override
    public synchronized boolean deleteTrader(String name) {
        if(!hasTrader(name)){
            return false;
        }
        traders.remove(name);
        System.out.println("Trader for"+name+"has been deleted");
        return true;
         
    }
    private boolean hasTrader(String name){
        if(traders.get(name)==null){
            return false;
        }else{
            return true;
        }
    }
    
    
}
